package com.zenith.storage.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.storage.po.dto.ProviderDTO;
import com.zenith.storage.po.domain.StorageProvider;
import com.zenith.storage.po.vo.StorageProviderVO;
import com.zenith.storage.service.StorageProviderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 存储提供商控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/storage/provider")
@RequiredArgsConstructor
public class StorageProviderController {
    
    private final StorageProviderService storageProviderService;
    
    /**
     * 获取所有存储提供商列表
     */
    @SecurePostMapping("/listProviders")
    public Result<List<StorageProviderVO>> listProviders() {
        try {
            List<StorageProviderVO> providers = storageProviderService.listProviders();
            return Result.success(providers);
        } catch (Exception e) {
            log.error("获取存储提供商列表失败", e);
            return Result.fail("获取存储提供商列表失败");
        }
    }
    
    /**
     * 添加存储提供商
     */
    @SecurePostMapping("/addProvider")
    public Result<Boolean> addProvider(@Valid @RequestBody ProviderDTO providerDTO) {
        try {
            StorageProvider provider = new StorageProvider();
            provider.setProviderName(providerDTO.getProviderName());
            provider.setEndpoint(providerDTO.getEndpoint());
            provider.setBucket(providerDTO.getBucket());
            provider.setAccessKey(providerDTO.getAccessKey());
            provider.setSecretKey(providerDTO.getSecretKey());
            provider.setRegion(providerDTO.getRegion());
            provider.setStatus(providerDTO.getStatus());
            provider.setIsDefault(providerDTO.getIsDefault());
            provider.setRemark(providerDTO.getRemark());
            
            Boolean result = storageProviderService.addProvider(provider);
            return Result.success(result);
        } catch (Exception e) {
            log.error("添加存储提供商失败", e);
            return Result.fail("添加存储提供商失败");
        }
    }
    
    /**
     * 更新存储提供商
     */
    @SecurePostMapping("/updateProvider")
    public Result<Boolean> updateProvider(@Valid @RequestBody ProviderDTO providerDTO) {
        try {
            StorageProvider provider = new StorageProvider();
            provider.setId(providerDTO.getId());
            provider.setProviderName(providerDTO.getProviderName());
            provider.setEndpoint(providerDTO.getEndpoint());
            provider.setBucket(providerDTO.getBucket());
            provider.setAccessKey(providerDTO.getAccessKey());
            provider.setSecretKey(providerDTO.getSecretKey());
            provider.setRegion(providerDTO.getRegion());
            provider.setStatus(providerDTO.getStatus());
            provider.setIsDefault(providerDTO.getIsDefault());
            provider.setRemark(providerDTO.getRemark());
            
            Boolean result = storageProviderService.updateProvider(provider);
            return Result.success(result);
        } catch (Exception e) {
            log.error("更新存储提供商失败", e);
            return Result.fail("更新存储提供商失败");
        }
    }
    
    /**
     * 删除存储提供商
     */
    @SecurePostMapping("/deleteProvider/{id}")
    public Result<Boolean> deleteProvider(@PathVariable Long id) {
        try {
            Boolean result = storageProviderService.deleteProvider(id);
            return Result.success(result);
        } catch (Exception e) {
            log.error("删除存储提供商失败", e);
            return Result.fail("删除存储提供商失败");
        }
    }
    
    /**
     * 设置默认存储提供商
     */
    @SecurePostMapping("/setDefaultProvider/{id}")
    public Result<Boolean> setDefaultProvider(@PathVariable Long id) {
        try {
            Boolean result = storageProviderService.setDefaultProvider(id);
            return Result.success(result);
        } catch (Exception e) {
            log.error("设置默认存储提供商失败", e);
            return Result.fail("设置默认存储提供商失败");
        }
    }
    
    /**
     * 获取默认存储提供商
     */
    @SecurePostMapping("/getDefaultProvider")
    public Result<StorageProviderVO> getDefaultProvider() {
        try {
            StorageProviderVO provider = storageProviderService.getDefaultProvider();
            return Result.success(provider);
        } catch (Exception e) {
            log.error("获取默认存储提供商失败", e);
            return Result.fail("获取默认存储提供商失败");
        }
    }
}